package com.xy.server;

import com.corundumstudio.socketio.*;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import com.xy.server.database.SqliteDBService;
import com.xy.server.service.FaceService;
import com.xy.server.service.MosaicService;
import com.xy.server.util.ServerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

@SpringBootApplication
@Service
public class XyprojectApplication {

//	@Value("${app.server.ip}")
	private String IP;

//	@Value("${app.server.port}")
	private Integer port;

//	@Value("${app.server.db}")
	private String db;

//	@Value("${app.server.resource}")
	private String resource;

	private String res_img;
	@Autowired
	FaceService faceService;

	@Autowired
	MosaicService mosaicService;

	@Bean
	public SocketIOServer socketIOServer() throws Exception
	{
		SimpleDateFormat myFmt1=new SimpleDateFormat("yyyy-MM-dd");
		Date now=new Date();
		System.out.println(myFmt1.format(now));


		db = "D:/Work/db.sqlite";
		resource="D:/Work/apache-tomcat-7.0.86-xy/webapps/res/";// + myFmt1.format(now) +"/";
		res_img="D:/Work/apache-tomcat-7.0.86-xy/webapps/res-img/";
		IP="127.0.0.1";
		port=9092;

//		db= "/Users/agan/IdeaProjects/XYProject/db.sqlite";
//		resource= "/Users/agan/IdeaProjects/apache-tomcat-8.5.23-xy/webapps/res/";
//		res_img="/Users/agan/IdeaProjects/apache-tomcat-8.5.23-xy/webapps/res-img/";
//		IP="127.0.0.1";
//		port=9092;

//		db= "/Users/agan/IdeaProjects/XYProject/db.sqlite";
//		resource= "/Users/agan/IdeaProjects/apache-tomcat-7.0.86-xy/webapps/res/";
//		res_img= "/Users/agan/IdeaProjects/apache-tomcat-7.0.86-xy/webapps/res-img/";
//		IP="127.0.0.1";
//		port=9092;

		new File(resource).mkdirs();



		ServerUtil.DBPath = db;
		ServerUtil.ResPath =  resource;
		ServerUtil.ResImgPath = res_img;
		ServerUtil.IP = IP;

		SqliteDBService.createConnection(ServerUtil.DBPath);
		faceService.init(ServerUtil.ResPath);
		mosaicService.init();

		ServerUtil.MosaicTemplatePath = ServerUtil.ResImgPath +"mosaic_template.jpg";
//        String mosaicFileName = MosaicService.mosaic();


		Configuration config = new Configuration();
		config.setHostname(ServerUtil.IP);
		config.setPort(port);

		//该处可以用来进行身份验证
//		config.setAuthorizationListener(new AuthorizationListener() {
//			@Override
//			public boolean isAuthorized(HandshakeData data) {
//				//http://localhost:8081?username=test&password=test
//				//例如果使用上面的链接进行connect，可以使用如下代码获取用户密码信息，本文不做身份验证
////              String username = data.getSingleUrlParam("username");
////              String password = data.getSingleUrlParam("password");
//				return true;
//			}
//		});

		final SocketIOServer server = new SocketIOServer(config);


		return server;
	}

	@Bean
	public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketServer) {
		return new SpringAnnotationScanner(socketServer);
	}


	public static void main(String[] args) {
		SpringApplication.run(XyprojectApplication.class, args);
	}
}
